import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams,ToastController} from 'ionic-angular';
import {LoadingService} from "../../../services/common/loading.service";
import {TimeUtil} from "../../../model/util/TimeUtil";
import {TeamOverviewProvider} from "../../../providers/perfomance/team-overview/team-overview-provider";

@IonicPage()
@Component({
    selector:'page-team-performance-member',
    templateUrl:'team-performance-member.html',
    providers:[
        LoadingService,
        TeamOverviewProvider
    ]
})

export class TeamPerformanceMemberPage {
    @ViewChild(Content) content: Content;

    private title:string = null;
    private groupID:string = null;
    private isLoaded:boolean = false;
    //开始时间
    private startFilterDate = '';
    //结束时间
    private endFilterDate = '';
    //分组业绩
    private groupList = [];

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        private load:LoadingService,
        private toastController:ToastController,
        private teamOverViewProvider:TeamOverviewProvider,
    ){}

    ionViewDidLoad(){
        //初始化时间范围（当月）
        this.groupID = this.navParams.get('groupID');
        this.title = this.navParams.get('groupName');

        this.startFilterDate = this.navParams.get('startFilterDate');
        this.endFilterDate = this.navParams.get('endFilterDate');

        this.filter();
    }

    filter(){
        if (this.endFilterDate != '' && this.startFilterDate != '' && this.endFilterDate < this.startFilterDate) {
            let toast = this.toastController.create({
                message: '结束时间不能小于开始时间，请重新选择',
                duration: 2000,
                position: 'middle'
            });
            toast.present();
            return;
        }
        this.isLoaded = false;
        this.load.showLoading();
        this.teamOverViewProvider.getMemberGroupList(this.groupID,this.startFilterDate,this.endFilterDate).subscribe(r=>{
            this.load.dismissLoading();
            this.isLoaded = true;
            this.groupList = r;
        },e=>{
            this.load.dismissLoading();
            this.isLoaded = true;
        });
        console.log('filter');
    }

    /**
     * 返回
     * @param ev
     */
    backButtonClick(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        this.navCtrl && this.navCtrl.pop(null, null);
    }

    /**
     * 跳转到分组列表
     * @param {any} item
     */
    goToMemberDetail(item:any){
        this.navCtrl.push('TeamPerformanceDetailPage',{
            groupID:item.agentCode,
            groupName:item.agentName,
            type:1,
            startFilterDate:this.startFilterDate,
            endFilterDate:this.endFilterDate
        });
    }

    /**
     * 移至顶部
     */
    goUp() {
        this.content.scrollToTop();
    }
}
